﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TomNet.IMSC.Core.Contracts.Common;
using TomNet.IMSC.Core.Contracts.LowPriceHouse;
using TomNet.IMSC.Web.Models.StaffApiParams;
using TomNet.Web.Mvc.UI;
using TomNet.Web.WebApi;

namespace TomNet.IMSC.Web.Controllers.StaffAPI
{
    public class StaffLowPriceHouseController : BaseApiController
    {
        public ILowPriceHousesContract LowPriceHousesContract { get; set; }
        public ILowPriceHouseBuildingsContract LowPriceHouseBuildingsContract { get; set; }
        public IImagesContract ImagesContract { get; set; }
        public IOwnerContract OwnerContract { get; set; }
        




        //低价房====================================================================================================

        [Description("API-低价房列表")]
        public IHttpActionResult ttGetBuildingsList([FromBody]LowPriceHousesParamsModel entity)
        {

            var query = from a in LowPriceHousesContract.Entities
                        join b in LowPriceHouseBuildingsContract.Entities on a.LowPriceHouseBuildingID equals b.Id
                        where (!a.IsDeleted) && a.LowHouseType == entity.LowHouseType && a.GroupID == entity.GroupID
                        select new
                        {
                            AgentID = a.AgentID,
                            Area = a.Area,
                            AreaID = b.AreaID,
                            AwardDescription = a.AwardDescription,
                            BalconyNumber = a.BalconyNumber,
                            BandSawBrokerage = a.BandSawBrokerage,
                            BrandAdvantage = b.BrandAdvantage,
                            Brokerage = a.Brokerage,
                            BuildingClass = a.BuildingClass,
                            BuildingsType = a.BuildingsType,
                            CarNumber = b.CarNumber,
                            Characteristic = b.Characteristic,
                            CityID = b.CityID,
                            CommissionAccount = a.CommissionAccount,
                            CookNumber = a.CookNumber,
                            CostPrice = a.CostPrice,
                            CoveredArea = b.CoveredArea,
                            CreatedTime = a.CreatedTime,
                            CreateYear = a.CreateYear,
                            DecorationCondition = a.DecorationCondition,
                            Developer = b.Developer,
                            DeveloperBrand = b.DeveloperBrand,
                            DownPaymentRate = a.DownPaymentRate,
                            GreeningRatio = b.GreeningRatio,
                            HallNumber = a.HallNumber,
                            HaveProductComparison = b.HaveProductComparison,
                            HouseNumber = a.HouseNumber,
                            HouseType = a.HouseType,
                            HouseTypeArea = b.HouseTypeArea,
                            HouseTypeRemark = a.HouseTypeRemark,
                            Id = a.Id,
                            Installation = a.Installation,
                            IsDeleted = a.IsDeleted,
                            IsLoan = a.IsLoan,
                            IsLocked = a.IsLocked,
                            LaunchTime = b.LaunchTime,
                            LivingFacilities = b.LivingFacilities,
                            LoanRules = a.LoanRules,
                            LowHouseType = a.LowHouseType,
                            LowPrice = a.LowPrice,
                            LowPriceHouseBuildingID = a.LowPriceHouseBuildingID,
                            BuildingName = b.Name,
                            BuildingAddress = b.Address,
                            MediumOfCommunicationStaffID = a.MediumOfCommunicationStaffID,
                            Nature = a.Nature,
                            NegotiatedPrice = a.NegotiatedPrice,
                            OpeningTime = b.OpeningTime,
                            Orientations = a.Orientations,
                            OwnerID = a.OwnerID,
                            ParkingRatio = b.ParkingRatio,
                            PlotRatio = b.PlotRatio,
                            Poundage = a.Poundage,
                            PriceAdvantage = b.PriceAdvantage,
                            PriceCondition = a.PriceCondition,
                            PropertyManagementCompany = b.PropertyManagementCompany,
                            PropertyManagementFee = b.PropertyManagementFee,
                            PropertyRight = a.PropertyRight,
                            PropertyType = a.PropertyType,
                            ProvinceID = b.ProvinceID,
                            RegionalDevelopment = b.RegionalDevelopment,
                            Remark = a.Remark,
                            ReNameDeadline = a.ReNameDeadline,
                            ResidenceType = a.ResidenceType,
                            RoomNumber = a.RoomNumber,
                            Rules = a.Rules,
                            SchoolDistrict = b.SchoolDistrict,
                            ShowIndex = a.ShowIndex,
                            StarTarget = a.StarTarget,
                            StarTargetRemark = a.StarTargetRemark,
                            State = a.State,
                            StationedStaffID = a.StationedStaffID,
                            Storey = a.Storey,
                            ToiletNumber = a.ToiletNumber,
                            TotalHouseholds = b.TotalHouseholds,
                            TotalStorey = a.TotalStorey,
                            Transportation = b.Transportation,
                            UseArea = a.UseArea,
                            X = b.X,
                            Y = b.Y

                        };


            //条件搜索
            if (!string.IsNullOrWhiteSpace(entity.KeyWord))
            {
                query = query.Where(m => m.BuildingName.Contains(entity.KeyWord));
            }

            var list = (from m in query
                        let img = (from m1 in ImagesContract.Entities where m1.GroupID == 4 && m1.InterfixID == m.Id && m1.IsDeleted == false orderby m1.Id select (m1.ImagePath + m1.FileName)).FirstOrDefault()
                        let ownerName = (from m2 in OwnerContract.Entities where m.OwnerID ==  m2.Id && m2.IsDeleted == false orderby m2.Id select m2.Name).FirstOrDefault()
                        select new LowPriceHousesListModel
                        {
                            Id = m.Id,
                            BuildingName = m.BuildingName,
                            CostPrice = m.CostPrice + "",//原价
                            LowPrice = m.LowPrice + "",//底价
                            Area = m.Area + "㎡",//面积
                            IsLoan = m.IsLoan == 0 ? "不可贷款" : "可以贷款",
                            Poundage = m.Poundage,//订金
                            Brokerage = m.Brokerage,//佣金
                            CommissionAccount = m.CommissionAccount,//佣金帐期
                            ImagePath = (img == null) ? "" : img.Replace(@"\", "/"),
                            OwnerName = ownerName == null ? "" : ownerName,//房主
                            CreatedTime = m.CreatedTime
                        });

            //数据总数
            var total = list.Count();

            //默认给一个排序的字段
            list = list.OrderByDescending(m => m.CreatedTime);

            //分页(假如total == 0，则取消分页查询，提高性能)
            list = total > 0 ? list.Skip((entity.PageIndex - 1) * entity.PageSize).Take(entity.PageSize)
                : Enumerable.Empty<LowPriceHousesListModel>().AsQueryable();// null;

            List<LowPriceHousesListModel> tmp = list.ToList();
            foreach (var item in tmp)
            {
                item.CostPrice = (Convert.ToDecimal(item.CostPrice) / 10000m).ToString("F2") + "万元";
                item.LowPrice = (Convert.ToDecimal(item.LowPrice) / 10000m).ToString("F2") + "万元";
            }

            var result = new AjaxResult("成功", AjaxResultType.Success, tmp);
            return Token(result, "");

        }





    }
}
